package com.marchsoft.group.manager.security.entity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * fetch
 * @Author zhanghaoqi
 * @Description     登陆时，访问数据库，返回的user对象的封装，这里继承了UserDetails
 * @Data 16:32 2020/12/18
 */
public class SecurityUserDetails implements UserDetails {
   private String  username;
   private String  password;
   private Collection<? extends GrantedAuthority> authorities;

    public SecurityUserDetails(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        this.username = username;
        this.password = password;
        this.authorities = authorities;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }
    //账户是否过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    //是否禁用
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    //密码是否过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    //是否启用
    @Override
    public boolean isEnabled() {
        return true;
    }
    //获取当前登陆者的信息
    //获得当前登陆用户对应的对象。
    public static SecurityUserDetails getCurUser(){
        System.out.println(SecurityContextHolder.getContext());
        SecurityUserDetails userDetails = (SecurityUserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        System.out.println("当前用户:"+userDetails);
        return userDetails;
    }
}
